﻿/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author
 */


Cognitum.Polizas.Siniestro.Beneficios.Componentes.Generales = Ext.extend(Karma.FB2, {

    divisionOperativaBeneficios: null,

    initComponent: function() {
        Ext.apply(this, {
            title: 'Datos generales',
            column1items: [{
                fieldLabel: 'Numero',
                name: 'Numero'
            }, {
                fieldLabel: 'IsComplemento',
                name: 'IsComplemento',
                hidden: true
            }, {
                fieldLabel: 'Fecha Siniestro',
                name: 'FechaSiniestro',
                xtype: 'datefield',
                allowBlank: false
            }, {
                fieldLabel: 'EstatusSiniestro',
                name: 'EstatusSiniestro',
                xtype: 'enum.combo',
                enumName: 'Cognitum.Dominio.Polizas.EstatusSiniestroEnum'
            }, {
                entityName: 'Certificados',
                xtype: 'entity.link',
                fieldLabel: 'Certificado',
                name: 'BienAsegurado'
            }, {
                fieldLabel: 'Tipo Siniestro',
                name: 'TipoSiniestro',
                xtype: 'enum.combo',
                enumName: 'Cognitum.Dominio.Polizas.Beneficios.TipoSiniestroBeneficiosEnum'
            }, {
                fieldLabel: 'Especialidad',
                name: 'Especialidad',
                xtype: 'enum.combo',
                enumName: 'Cognitum.Dominio.Polizas.Beneficios.EspecialidadEnum'
            }, {
                fieldLabel: 'Padecimiento',
                name: 'Padecimiento',
                xtype: 'textarea'
            }, {
                fieldLabel: 'Contacto',
                name: 'Contacto',
                entityName: 'ContactoSimple',
                xtype: 'entity.link'
            }],
            column2items: [
                {
                fieldLabel: 'Tipo Reclamacion',
                name: 'TipoReclamacion',
                xtype: 'enum.combo',
                enumName: 'Cognitum.Dominio.Polizas.Beneficios.TipoReclamacionEnum'
            },{
                    fieldLabel: 'Total Reclamado',
                    name: 'TotalReclamado',
                    xtype: 'numberfield',
                    vtype: 'numericMask',
                    enableKeyEvents: true,
                    listeners: {
                        'keyup': {
                            fn: this.onKeyUp2,
                            scope: this
                        }
                    }
                }, {
                    fieldLabel: 'Gastos No Cubiertos',
                    name: 'GastosNoCubiertos',
                    xtype: 'numberfield',
                    vtype: 'numericMask',
                    enableKeyEvents: true,
                    listeners: {
                        'keyup': {
                            fn: this.onKeyUp2,
                            scope: this
                        }
                    }
                }, {
                    fieldLabel: 'Deducible %',
                    name: 'DeduciblePct',
                    xtype: 'numberfield',
                    vtype: 'numericMask',
                    enableKeyEvents: true,
                    listeners: {
                        'keyup': {
                            fn: this.onKeyUp2,
                            scope: this
                        }
                    }
                }, {
                    fieldLabel: 'Deducible',
                    name: 'Deducible',
                    xtype: 'numberfield',
                    vtype: 'numericMask',
                    enableKeyEvents: true,
                    listeners: {
                        'keyup': {
                            fn: this.onKeyUp2,
                            scope: this
                        }
                    }
                }, {
                    fieldLabel: 'Coaseguro %',
                    name: 'CoaseguroPct',
                    xtype: 'numberfield',
                    vtype: 'numericMask',
                    enableKeyEvents: true,
                    listeners: {
                        'keyup': {
                            fn: this.onKeyUp2,
                            scope: this
                        }
                    }
                }, {
                    fieldLabel: 'Coaseguro',
                    name: 'Coaseguro',
                    xtype: 'numberfield',
                    vtype: 'numericMask',
                    enableKeyEvents: true,
                    listeners: {
                        'keyup': {
                            fn: this.onKeyUp2,
                            scope: this
                        }
                    }
                }, {
                    fieldLabel: 'Total a Pagar',
                    name: 'TotalAPagar',
                    readonly: true
                }, {
                    fieldLabel: 'Total Anticipado',
                    name: 'TotalAnticipado',
                    disabled: true
                }, {
                    fieldLabel: 'Total Pagado',
                    name: 'TotalPagado',
                    disabled: true
                }, {
                    fieldLabel: 'Monto Restante',
                    name: 'MontoRestante',
                    disabled: true
                }, {
                    entityName: 'Siniestros',
                    xtype: 'entity.link',
                    fieldLabel: 'Siniestro',
                    name: 'Siniestro',
                    disabled: true
                }, {
                    entityName: 'Polizas',
                    xtype: 'entity.link',
                    fieldLabel: 'Poliza',
                    name: 'Poliza',
                    disabled: true
}]
                });
                Cognitum.Polizas.Siniestro.Beneficios.Componentes.Generales.superclass.initComponent.apply(this, arguments);
            },

            onKeyUp2: function(field, e) {
                this.calculaMonto(field);
            },
            
            onLoad: function(value) {
                this.calculaMonto(null);
            },

            calculaMonto: function(field) {
                var TotalAPagar = this.getForm().findField('TotalAPagar');
                var Coaseguro = this.getForm().findField('Coaseguro');
                var CoaseguroPct = this.getForm().findField('CoaseguroPct');
                var Deducible = this.getForm().findField('Deducible');
                var DeduciblePct = this.getForm().findField('DeduciblePct');
                var TotalReclamado = this.getForm().findField('TotalReclamado');
                var GastosNoCubiertos = this.getForm().findField('GastosNoCubiertos');
                var TotalAnticipado = this.getForm().findField('TotalAnticipado');
                var MontoRestante = this.getForm().findField('MontoRestante');
                var TotalPagado = this.getForm().findField('TotalPagado');
                var subTotal = TotalReclamado.getValue() - GastosNoCubiertos.getValue();

                if (!Ext.isEmpty(field) && field.getName() == 'Coaseguro') {
                    var valor = (parseFloat(Coaseguro.getValue()) * 100) / parseFloat(subTotal);
                    if (valor == 'NaN') {
                        CoaseguroPct.setValue('');
                    }
                    else {
                        CoaseguroPct.setValue(valor);
                    }
                }
                else {
                    var valor = ((parseFloat(CoaseguroPct.getValue()) / 100) * parseFloat(subTotal));
                    if (valor == 'NaN') {
                        Coaseguro.setValue('');
                    }
                    else {
                        Coaseguro.setValue(valor);
                    }
                }

                if (!Ext.isEmpty(field) && field.getName() == 'Deducible') {
                    var valor = (parseFloat(Deducible.getValue()) * 100) / parseFloat(subTotal);
                    if (valor == 'NaN') {
                        DeduciblePct.setValue('');
                    }
                    else {
                        DeduciblePct.setValue(valor);
                    }
                }
                else {
                    var valor = ((parseFloat(DeduciblePct.getValue()) / 100) * parseFloat(subTotal));
                    if (valor == 'NaN') {
                        Deducible.setValue('');
                    }
                    else {
                        Deducible.setValue(valor);
                    }
                }
                TotalAPagar.setValue(subTotal - Deducible.getValue() - Coaseguro.getValue() - TotalAnticipado.getValue());
                MontoRestante.setValue(TotalAPagar.getValue() - TotalPagado.getValue());
            }

        });

Ext.reg('comp.ctls.gral', Cognitum.Polizas.Siniestro.Beneficios.Componentes.Generales);

 
 
Cognitum.Polizas.Siniestro.Beneficios.Complemento.Editor = Ext.extend(Karma.EBCard, {

    initComponent: function() {
    var _sections = [
        {
                title: 'Datos generales',
                xtype: 'comp.ctls.gral'
            }, {
                title: 'Bitacora',
                items: [{
                    hideLabel: true,
                    name: 'Bitacora',
                    entityName: 'BitacoraSiniestro',
                    xtype: 'ag.list',
                    context: 'b.Siniestro.Id=?',
                    anchor: '99% 99%'
                }]
            }, {
                title: 'Pagos y Anticipos',
                items: [{
                    hideLabel: true,
                    name: 'Pagos',
                    entityName: 'PagoBeneficio',
                    xtype: 'ag.list',
                    context: 'p.Siniestro.Id=?',
                    anchor: '99% 99%',
                    listeners: {
                        refresh: {
                            fn: function() {
                                this.entityId = this.value.Id;
                                this.loadAction();
                            },
                            scope: this
                        }
                    }
                }]
            }, {
                xtype: 'aud.editor',
                title: 'Auditoria'
            }
    ];
    if (this.seccionesAdicionales) {
            Ext.each(this.seccionesAdicionales, function(item){
                _sections.push(item);
            }, this);
        }
        Ext.apply(this, {
            sections: _sections
        });
    
        Cognitum.Polizas.Siniestro.Beneficios.Complemento.Editor.superclass.initComponent.apply(this, arguments);
    },
    
    getTBarDynamicControls: function() {
        if (this.value.Id > 0) {
            var cartaEntity = Karma.Core.ModuleManager.Instance.getEntity('Cartas');
            return [{
                fn: cartaEntity.getMenuComplementoSin,
                params: this.value.Id,
                scope: cartaEntity
                }];
            }
        }
});

Ext.reg('sin.benef.comp.editor', Cognitum.Polizas.Siniestro.Beneficios.Complemento.Editor);
